import axios from 'axios'
import { baseURL } from '@/config'
class HttpRequest {
    constructor (baseUrl = baseURL){
        this.baseUrl = baseUrl
        this.queue = {}
    }
    getInsideConfig (){
        const config = {
            baseURL: this.baseUrl,
            headers: {
                
            }
        }
        return config
    }
    interceptors(instance, url){
        instance.interceptors.request.use(config => {
            //添加全局的loding
            if(!Object.keys(this.queue).length)
            this.queue[url] = true
            console.log('请求中')
            return config
        },error => {
            return Promise.reject(error)            
        })
        instance.interceptors.response.use(res => {
            delete this.queue[url]
            console.log('请求结束')
            return res
        },error=>{
            delete this.queue[url]
            return Promise.reject(error)
        })
    }
    request(options){
        const instance = axios.create()
        options = Object.assign(this.getInsideConfig(),options)
        this.interceptors(instance, options.url)
        return instance(options)
    }
}
export default HttpRequest